using System.Net.DnsToolkit.Packet;
using System.Net.DnsToolkit.Util;
using System.Net.DnsToolkit.Serializer.Stream.Extension;
using System.Net.DnsToolkit.Packet.Busy;

namespace System.Net.DnsToolkit.Serializer.Stream.Reader {
    public class QuestionReader : IQuestionReader<System.IO.Stream> {

        public IQuestion Read(System.IO.Stream source) {
            IQuestion q;
            Read(false, source, out q);
            return q;
        }

        public bool Read(bool tryRead, System.IO.Stream source, out IQuestion question) {

            IDomainName domn = source.ReadDomainName();
            ushort qtype = source.Read16();
            ushort qclass = source.Read16();

            var q = new Question {
                QName = domn,
                QType = Enum<QTypeEnum>.ToObject(qtype),
                QClass = Enum<QClassEnum>.ToObject(qclass),
            };

            question = q;
            return true;

        }

        public bool TryRead(System.IO.Stream source, out IQuestion question) {
            return Read(true, source, out question);
        }
    }
}